mui.init({
	/*beforeback:function() {
		var parentObj = plus.webview.currentWebview().opener();
		mui.fire(parentObj, 'dataList');
	}*/
});

document.addEventListener("swipeleft", function () {
    console.log('左');
    if(kdjjDetails.tabIndex < 3){
        kdjjDetails.tabIndex ++;
    }else{
        kdjjDetails.tabIndex = 3
    }
	mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
});

document.addEventListener("swiperight", function () {
    console.log('右');
    if(kdjjDetails.tabIndex > 1){
        kdjjDetails.tabIndex --;
    }else{
        kdjjDetails.tabIndex = 1
    }
    mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
});


document.addEventListener("swipeup", function () {
    if(mui('#scroll').scroll().y <= mui('#scroll').scroll().maxScrollY){
        kdjjDetails.getKdDetails('up');
    }
});


document.addEventListener("touchstart", function (event) {
    console.log('开始触摸屏幕');
});


document.addEventListener("touchmove", function (event) {
    if(kdjjDetails.openDown == 1){
        if(mui('#scroll').scroll().y >= 0){
            kdjjDetails.downData = 1;
            kdjjDetails.downText = '下拉以刷新数据...'
        }
        if(mui('#scroll').scroll().y >= 15){
            kdjjDetails.downData = 1;
            kdjjDetails.downText = '松开手指以刷新...'
        }
    }
});

document.addEventListener("touchend", function (event) {
    if(mui('#scroll').scroll().y >= 20){
        kdjjDetails.downData = 0;
        kdjjDetails.getKdDetails('down');
    }
    if(mui('#scroll').scroll().y > 0){
        kdjjDetails.openDown = 1;
    }else{
        kdjjDetails.openDown = 0;
    }
});

var kdjjDetails;
var page = 1;
mui.plusReady(function () {
    kdjjDetails = new Vue({
        el: '#list_box',
        data: {
            hexin_list: [],
            shengshi_list: [],
            zhiwei_list: [],
            upData:0,
            downData:0,
            downText:'',
            openDown:1,  //该状态是否可以激活下拉加载
            
            tabIndex: '1',
            selDiqu: 1,
            selDiquName: '',
            selZhiwei: '',
            city: JSON.parse(plus.storage.getItem('area')),
            zhiwei: JSON.parse(plus.storage.getItem('zhiwei')),
        },
        mounted() {
            this.getKdDetails('normal');
            this.$nextTick(function () {
                mui('.mui-scroll-wrapper').scroll({
                	indicators: false,
                    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                });
            })
        },
        methods: {
            getKdDetails: function (type) {
                if(type == 'up'){
                    page++;
                    this.upData = 1;
                }else if(type == 'down'){
                    page = 1;
                    this.hexin_list.length = 0;
                    this.shengshi_list.length = 0;
                    this.zhiwei_list.length = 0;
                }else if(type == 'normal'){
                    showWatting();
                }
                var api_host = plus.storage.getItem('HOST_URL');
                var area_id = this.selDiqu ? this.selDiqu : '';
                var area_name = this.selDiquName ? this.selDiquName : '';
                var zhiwei = this.selZhiwei ? this.selZhiwei : '';
                mui.ajax({
                    url: api_host + '/api/linxuan/news/allexampoint',
                    data: {
                        'currentpage': page,
                        'area_id': area_id,
                        'area_name': area_name,
                        'zhiwei': zhiwei
                    },
                    dataType: 'json',
                    type: 'get',
                    timeout: 3000,
                    success: function (msg) {
                        if(msg.code == 1){
                            switch (type) {
                                case 'up':
                                    $.each(msg.data.hexin.text, function (index, value) {
                                        kdjjDetails.hexin_list.text.push(value);
                                    })
                                    $.each(msg.data.shengshi.text, function (index, value) {
                                        kdjjDetails.shengshi_list.text.push(value);
                                    })
                                    $.each(msg.data.zhiwei.text, function (index, value) {
                                        kdjjDetails.zhiwei_list.text.push(value);
                                    })
                                    break;
                                case 'down':
                                    kdjjDetails.hexin_list = msg.data.hexin;
                                    kdjjDetails.shengshi_list = msg.data.shengshi;
                                    kdjjDetails.zhiwei_list = msg.data.zhiwei;
                                    mui.toast('刷新成功!', {duration: '200',type: 'div'});
                                    break;
                                case 'normal':
                                    kdjjDetails.hexin_list = msg.data.hexin;
                                    kdjjDetails.shengshi_list = msg.data.shengshi;
                                    kdjjDetails.zhiwei_list = msg.data.zhiwei;
                                    break;
                            }
                        }else{
                            mui.toast(msg.msg, {duration: '200',type: 'div'});
                        }

                    },
                    error: function (xhr,type,errorThrown) {
                        console.log(xhr);
                        mui.toast('加载失败!', {duration: '200',type: 'div'});
                    },
                    complete:function (XMLHttpRequest,status) {
                        plus.nativeUI.closeWaiting();
                        if(type == 'up'){
                            newsData.upData = 0;
                        }
                        if(status == 'timeout'){//超时,status还有success,error等值的情况
                            mui.toast('请求超时，请重试', {duration: '300',type: 'div'});
                        }
                    }
                })
            },
            open_details: function (id,zhiwei) {
                mui.openWindow({
                    url: 'detail.html', 
                    extras: {
                        'newsid': id,
						'zhiwei':zhiwei
                    },
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                        title: '加载中...',
                        options: {},
                    }
                });
            },
            select_tab: function (e) {
                this.tabIndex = e.target.getAttribute('tab-index');
                mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
            },
            yincangShaixuan: function () {
                if (this.tabIndex != 3) {
                    $('.popMask').hide();
                    $('.popFilter').animate({
                        'right': '-90%'
                    }, 200);
                } else {
                    $('.popMask').hide();
                    $('.popFilter2').animate({
                        'right': '-90%'
                    }, 200);
                }
            },
            quedingShaixuan: function (ty) {
                $('.popMask').hide();
                if(ty == 'dq'){
                    $('.popFilter').animate({
                        'right': '-90%'
                    }, 200);
                }else if(ty == 'zw'){
                    $('.popFilter2').animate({
                        'right': '-90%'
                    }, 200);
                }
                mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);//100毫秒滚动到顶
                this.getKdDetails('down');
            },
            select_diqu: function (e) {
                this.selDiqu = e.target.getAttribute('diqu-index');
                this.selDiquName = e.target.getAttribute('diqu-name');
            },
            select_zhiwei: function (e) {
                this.selZhiwei = e.target.getAttribute('zhiwei-name');
            },
        }
    })
})

function open_shaixuan() {
    if(kdjjDetails.tabIndex != 3){
        $('.popMask').show();
        $('.popFilter').animate({
            'right': '0%'
        }, 200);
    }else{
        $('.popMask').show();
        $('.popFilter2').animate({
            'right': '0%'
        }, 200);
    }
}